package tech.horgee.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import tech.horgee.common.IWrapper;
import tech.horgee.group.InsertGroup;

import java.io.Serializable;
import java.util.Objects;

/**
 * 用户和角色关联表
 *
 * @author haojun
 * @since 2022-06-14
 */
@Getter
@Setter
@TableName("tab_user_role")
public class UserRole implements Serializable, IWrapper<UserRole> {

    private static final long serialVersionUID = 1L;

    /**
     * 用户ID
     */
    @TableId(type = IdType.INPUT)
    @NotNull(groups = InsertGroup.class)
    private Long userId;

    /**
     * 角色ID
     */
    @NotNull(groups = InsertGroup.class)
    private Long roleId;

    @Override
    public LambdaQueryWrapper<UserRole> wrapper() {
        return new LambdaQueryWrapper<UserRole>()
                .eq(Objects.nonNull(userId), UserRole::getUserId, userId)
                .eq(Objects.nonNull(roleId), UserRole::getRoleId, roleId)
                ;
    }
}
